home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / examples / infobrws / src / mci.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-02  |  30.8 KB  |  1,265 lines

  1. //     (C) Copyright Microsoft Corp. 1991.  All rights reserved.
  2. //
  3. //     You have a royalty-free right to use, modify, reproduce and 
  4. //     distribute the Sample Files (and/or any modified version) in 
  5. //     any way you find useful, provided that you agree that 
  6. //     Microsoft has no warranty obligations or liability for any 
  7. //     Sample Application Files which are modified. 
  8.  
  9. /****************************************************************************
  10.  
  11.     MODULE    : mci.c
  12.  
  13.     PURPOSE   : Module which contains the MCI functionality of the playvfw application.  All Video for 
  14.                 Windows as well as audio calls are contained in this module.  
  15.  
  16.     FUNCTIONS : 
  17.     
  18.                 OpenVFWDevice
  19.                 OpenVFWFile
  20.                 PlayVFWFile
  21.                 PlayVFWFileWait
  22.                 CloseVFWFile
  23.                 CloseAllDevices
  24.                 SeekVFWToStart
  25.                 StepVFW
  26.                 StepVFWReverse
  27.                 UpdateVFW
  28.                 PauseVFWFile
  29.                 StopVFWFile
  30.                 ResumeVFWFile
  31.                 CloseVFWDevice
  32.                 OpenPlayCloseVFW
  33.                 SndPlaySnd   
  34.                 ErrorProc
  35.                 OpenWaveDevice
  36.                 PlayWaveFile
  37.                 StopWaveFile
  38.                 CloseWaveFile
  39.                 CloseWaveDevice
  40.                 cNumAudio
  41.  
  42.     COMMENTS  : 
  43.  
  44.     HISTORY   :
  45.  
  46. ****************************************************************************/
  47.  
  48. #include <windows.h>
  49. #include <stdio.h>
  50. #include <mmsystem.h>
  51. #include <digitalv.h>
  52. #include "playvfw.h" 
  53. #include "proto.h"
  54.  
  55.  
  56. // forward declarations
  57.  
  58. /****************************************************************************
  59.  
  60.     FUNCTION  :  OpeVFWDevice(HWND)
  61.  
  62.     PURPOSE   :  This function opens the device avivideo and leaves it open.
  63.          
  64.  
  65.          mciopen.lpstrDeviceType="avivideo"
  66.  
  67.          Using this format you only have to specify
  68.  
  69.          MCI_OPEN_TYPE for dwflags.
  70.  
  71.     COMMENTS  :  Opening the driver once and leaving it open saves time.
  72.          You can then use this driver ID in the following calls
  73.          to open elements (AVI videos).  This saves time since if
  74.          you open an element without specifying an open device ID
  75.          mci must open the driver and close the driver each time.
  76.          When playing multiple elements (AVI videos) this excess time
  77.          can be costly.  Therefore to conserve time you should open
  78.          the avivideo device, play the AVI videos, close the avivideo
  79.          device.
  80.  
  81.  
  82.     HISTORY   :
  83.  
  84. ****************************************************************************/
  85.  
  86.  
  87. WORD FAR PASCAL OpenVFWDevice(hWnd)
  88.  
  89. HWND hWnd;
  90. {
  91.  
  92.      MCI_DGV_OPEN_PARMS mciopen;
  93.      DWORD dwRes;
  94.      WORD wGlobalDeviceID;
  95.      DWORD dwFlags;
  96.      HANDLE hMem;
  97.      LPSTR lpMem;
  98.  
  99.      // no device ID is necessary when opening using DeviceType.
  100.      hMem=GlobalAlloc(GHND,80);
  101.      lpMem=GlobalLock(hMem);
  102.      lstrcpy(lpMem,(LPSTR)"avivideo");
  103.      
  104.      mciopen.wDeviceID=0;
  105.      //mciopen.lpstrDeviceType=(LPSTR)"avivideo";
  106.      mciopen.lpstrDeviceType=lpMem;
  107.      //mciopen.lpstrAlias=(LPSTR)"";
  108.      
  109.      // Set the device to be shareable so that several AVI videos can be open at a single time.
  110.      // MCI_OPEN_TYPE specifies that the lpstrDeviceType string is to be used to specify what device
  111.      // is to be open.
  112.      
  113.      dwFlags= MCI_OPEN_TYPE | MCI_OPEN_SHAREABLE;
  114.                       
  115.      // Send the command to open the device.  dwRes returns an error number if the command fails otherwise
  116.      // it returns 0.                 
  117.                       
  118.                       
  119.      dwRes=mciSendCommand(0,MCI_OPEN,dwFlags,(DWORD)(LPSTR)&mciopen);
  120.  
  121.      if (dwRes)
  122.           {
  123.           ErrorProc(dwRes);
  124.           return(FALSE);
  125.           }   
  126.           
  127.      GlobalUnlock(hMem);
  128.      GlobalFree(hMem);     
  129.      
  130.      // retrieve the device ID from the mciopen structure.
  131.      wGlobalDeviceID=mciopen.wDeviceID;
  132.  
  133.      return(wGlobalDeviceID);
  134.      }
  135.                                                           
  136.                                                           
  137. /****************************************************************************
  138.  
  139.     FUNCTION  :  OpenVFWFile(HWND,DWORD,LPSTR,LPSTR)
  140.  
  141.     PURPOSE   :  This function takes a currently open device ID, a
  142.          string representing a AVI video, a handle to a window which could
  143.          be omitted since it is not used and opens that AVI Video.
  144.          
  145.  
  146.     COMMENTS  :  This function is used to open a AVI video for later playback.  The function returns
  147.      a device ID which can be used later to playback the video.
  148.  
  149.     HISTORY   :
  150.  
  151. ****************************************************************************/
  152.  
  153.  
  154. WORD FAR PASCAL OpenVFWFile(hWnd,wGlobalDeviceID,lpVFWName,lpVFWAlias)
  155.  
  156. HWND hWnd;
  157. WORD wGlobalDeviceID;
  158. LPSTR lpVFWName;
  159. LPSTR lpVFWAlias;
  160. {
  161.  
  162.      MCI_DGV_OPEN_PARMS mciopen;
  163.      DWORD dwRes;
  164.      DWORD dwFlags;
  165.      //MCI_DGV_WINDOW_PARMS mciWindow;
  166.  
  167.  
  168.  
  169.      // Set the flags so that we open an element, AVI video, and use an alias.  By using an alias
  170.      // we avoid colliding with other attempts to open the same video.
  171.  
  172.      
  173.      dwFlags= MCI_OPEN_ELEMENT | MCI_OPEN_ALIAS;
  174.  
  175.      mciopen.lpstrElementName=(LPSTR)lpVFWName;
  176.  
  177.      
  178.  
  179.      mciopen.lpstrDeviceType=NULL;
  180.      mciopen.lpstrAlias=lpVFWAlias;
  181.  
  182.       // Send the command to open the device.  dwRes returns an error number if the command fails otherwise
  183.      // it returns 0. 
  184.      
  185.      dwRes=mciSendCommand(wGlobalDeviceID,MCI_OPEN,dwFlags,(DWORD)(LPSTR)&mciopen);
  186.  
  187.  
  188.      if (dwRes)
  189.           {
  190.            ErrorProc(dwRes);
  191.            return FALSE;
  192.           }    
  193.  
  194.      
  195.      return mciopen.wDeviceID;
  196.      }
  197.  
  198. /****************************************************************************
  199.  
  200.     FUNCTION  :  PlayVFWFile(HWND,HWND,DWORD)
  201.  
  202.     PURPOSE   :  This function takes a currently open device ID, a handle to a window
  203.          that will be notified when playback is finished and a handle to a Window the 
  204.          AVI file will play in and calls the MCI_PLAY command.  The function immediately 
  205.          returns and does not wait for the AVI file to finish playing.
  206.  
  207.     COMMENTS  :  dwFlags is set to MCI_DGV_WINDOW_HWND so that the function will play the video
  208.          in a window specified by the handle passed to the function.  hWnd1 is the handle to the 
  209.          window in which you will be playing the video into.  Specifying a seperate window to play
  210.          the video in must be done before the play command.  hWnd is the callback window which will
  211.          be notified once the video is finished playing.
  212.  
  213.     HISTORY   :
  214.  
  215. ****************************************************************************/
  216.  
  217.  
  218. BOOL FAR PASCAL PlayVFWFile(hWnd,hWnd1,wDeviceID)
  219.  
  220. HWND hWnd;
  221. HWND hWnd1;
  222. WORD wDeviceID;
  223.  
  224. {
  225.  
  226.      MCI_DGV_PLAY_PARMS mciplay;
  227.      DWORD dwRes;
  228.      DWORD dwFlags;
  229.      MCI_DGV_WINDOW_PARMS mciWindow;
  230.  
  231.      if (!wDeviceID)
  232.           return FALSE;
  233.  
  234.      // specify the HWND which the video should be played into.
  235.      
  236.      mciWindow.hWnd=hWnd1;    
  237.                          
  238.      // Send a command to AVI specifying that the playback window will be defined by the HWND
  239.      // passed.                     
  240.                          
  241.      dwRes=mciSendCommand(wDeviceID,MCI_WINDOW,MCI_DGV_WINDOW_HWND,(DWORD)(LPSTR)&mciWindow);
  242.  
  243.      if (dwRes)
  244.           {
  245.            ErrorProc(dwRes);
  246.            return FALSE;
  247.            }
  248.  
  249.      mciplay.dwFrom=0;
  250.      
  251.      // use a callback window specified by the hWnd paramater.
  252.      
  253.      mciplay.dwCallback = MAKELONG(hWnd,0);
  254.  
  255.  
  256.      dwFlags=MCI_NOTIFY;
  257.  
  258.      dwRes=mciSendCommand(wDeviceID,MCI_PLAY,dwFlags,(DWORD)(LPSTR)&mciplay);
  259.  
  260.      if (dwRes)
  261.             {
  262.              ErrorProc(dwRes);
  263.              return FALSE;
  264.             }
  265.      
  266.  
  267.      return TRUE;
  268.      }
  269.  
  270. /****************************************************************************
  271.  
  272.     FUNCTION  :  PlayVFWFile(HWND,HWND,DWORD)
  273.  
  274.     PURPOSE   :  This function takes a currently open device ID, a handle to a window
  275.          that will be notified when playback is finished and a handle to a Window the 
  276.          AVI file will play in and calls the MCI_PLAY command.  The function immediately 
  277.          returns and does not wait for the AVI file to finish playing.
  278.  
  279.     COMMENTS  :  dwFlags is set to MCI_DGV_WINDOW_HWND so that the function will play the video
  280.          in a window specified by the handle passed to the function.  hWnd1 is the handle to the 
  281.          window in which you will be playing the video into.  Specifying a seperate window to play
  282.          the video in must be done before the play command.  hWnd is the callback window which will
  283.          be notified once the video is finished playing. Since the MCI_WAIT flag is being used the 
  284.          hWnd paramater is unecessary.
  285.  
  286.     HISTORY   :
  287.  
  288. ****************************************************************************/
  289.  
  290.  
  291. BOOL FAR PASCAL PlayVFWFileWait(hWnd,hWnd1,wDeviceID)
  292.  
  293. HWND hWnd;
  294. HWND hWnd1;
  295. WORD wDeviceID;
  296.  
  297. {
  298.  
  299.      MCI_DGV_PLAY_PARMS mciplay;
  300.      DWORD dwRes;
  301.      DWORD dwFlags;
  302.      MCI_DGV_WINDOW_PARMS mciWindow;
  303.  
  304.  
  305.      if (!wDeviceID)
  306.           return FALSE;                      
  307.                            
  308.      // specify the HWND which the video should be played into.
  309.      mciWindow.hWnd=hWnd1;  
  310.      
  311.      // Send a command to AVI specifying that the playback window will be defined by the HWND
  312.      // passed.     
  313.      
  314.      dwRes=mciSendCommand(wDeviceID,MCI_WINDOW,MCI_DGV_WINDOW_HWND,(DWORD)(LPSTR)&mciWindow);
  315.  
  316.      if (dwRes)
  317.          {
  318.           ErrorProc(dwRes);
  319.           return FALSE;
  320.          } 
  321.  
  322.      mciplay.dwFrom=0;
  323.      
  324.      // set the flag to wait until playback is finished before allowing mciSendCommand to return.
  325.      
  326.      dwFlags=MCI_WAIT;
  327.  
  328.      dwRes=mciSendCommand(wDeviceID,MCI_PLAY,dwFlags,(DWORD)(LPSTR)&mciplay);
  329.  
  330.      if (dwRes)
  331.          {
  332.           ErrorProc(dwRes);
  333.           return FALSE;
  334.          }
  335.  
  336.  
  337.      return TRUE;
  338.      }
  339.  
  340. /****************************************************************************
  341.  
  342.     FUNCTION  :  CloseVFWFile(HWND,WORD,LPSTR)
  343.  
  344.     PURPOSE   :  This function takes a currently open device ID and closes the device
  345.          element (AVI file).
  346.          
  347.  
  348.     COMMENTS  :  
  349.  
  350.     HISTORY   :
  351.  
  352. ****************************************************************************/
  353.  
  354.  
  355. BOOL FAR PASCAL CloseVFWFile(wDeviceID)
  356.  
  357. WORD wDeviceID;
  358. {
  359.  
  360.      DWORD dwRes;
  361.                        
  362.      if (!wDeviceID)
  363.           return FALSE;                  
  364.                        
  365.      dwRes=mciSendCommand(wDeviceID,MCI_CLOSE,0,NULL);
  366.  
  367.      if (dwRes)
  368.           {
  369.            ErrorProc(dwRes);
  370.            return FALSE;
  371.           }
  372.  
  373.      return TRUE;
  374. }
  375.      
  376.      
  377. /****************************************************************************
  378.  
  379.     FUNCTION  :  CloseAllDevices()
  380.  
  381.     PURPOSE   :  This function closes all currently open devices for the application. 
  382.          
  383.  
  384.     COMMENTS  :  
  385.  
  386.     HISTORY   :
  387.  
  388. ****************************************************************************/
  389.      
  390. BOOL FAR PASCAL CloseAllDevices()
  391.  
  392.  
  393. {
  394.      DWORD dwRes;
  395.                        
  396.                      
  397.                        
  398.                        
  399.      // Close the global device
  400.                        
  401.                        
  402.                        
  403.       dwRes=mciSendCommand(MCI_ALL_DEVICE_ID,MCI_CLOSE,0,NULL);
  404.  
  405.      if (dwRes)
  406.          {
  407.           ErrorProc(dwRes);
  408.           return(FALSE);
  409.          }
  410.  
  411.  
  412.  
  413.      return(TRUE);
  414.      }
  415.  
  416.  
  417.  
  418. /****************************************************************************
  419.  
  420.     FUNCTION  :  SeekVFWFile(WORD)
  421.  
  422.     PURPOSE   :  This function takes a currently open device ID and seeks
  423.          to the start of the AVI file.
  424.          
  425.  
  426.     COMMENTS  :  
  427.  
  428.     HISTORY   :
  429.  
  430. ****************************************************************************/
  431.  
  432.  
  433. BOOL FAR PASCAL SeekVFWToStart(wDeviceID)
  434. WORD wDeviceID;
  435. {
  436.      DWORD dwRes;      
  437.      
  438.      if (!wDeviceID)
  439.           return FALSE;
  440.      
  441.      dwRes=mciSendCommand(wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, (DWORD)(LPVOID)NULL);
  442.  
  443.      if (dwRes)
  444.           {
  445.            ErrorProc(dwRes);
  446.            return FALSE;
  447.      }
  448.  
  449.      return TRUE;
  450. }       
  451.  
  452. /****************************************************************************
  453.  
  454.     FUNCTION  :  StepVFW(WORD)
  455.  
  456.     PURPOSE   :  This function takes a currently open device ID and steps the AVI file
  457.      to the frame specified by nFrames.
  458.          
  459.  
  460.     COMMENTS  :  
  461.  
  462.     HISTORY   :
  463.  
  464. ****************************************************************************/
  465.  
  466.  
  467. BOOL FAR PASCAL StepVFW(wDeviceID,nFrames)
  468. WORD wDeviceID;   
  469. int nFrames;
  470. {
  471.      DWORD dwRes;
  472.      MCI_DGV_STEP_PARMS mciStep;
  473.                        
  474.      if (!wDeviceID)
  475.           return FALSE;                  
  476.                        
  477.      mciStep.dwFrames=nFrames;                                                           
  478.      
  479.      // This functions will return error codes when you step past the end of the Video.  To avoid
  480.      // the constant error messages recieved I chose to not return the string provided by mciErrorString.
  481.      
  482.      dwRes=mciSendCommand(wDeviceID, MCI_STEP, MCI_DGV_STEP_FRAMES, (DWORD)(LPSTR)&mciStep);
  483.  
  484.      if (dwRes)
  485.            {    
  486.             return FALSE;
  487.            }
  488.  
  489.      return TRUE;
  490. }
  491.  
  492. /****************************************************************************
  493.  
  494.     FUNCTION  :  StepVFWReverse(WORD)
  495.  
  496.     PURPOSE   :  This function takes a currently open device ID and steps the video
  497.     back 1 frame.  
  498.  
  499.     COMMENTS  :   Since you cannot specify how many frames in reverse you would like
  500.     to step I left off the nFrames parameter.  
  501.  
  502.     HISTORY   :
  503.  
  504. ****************************************************************************/
  505.  
  506.  
  507. BOOL FAR PASCAL StepVFWReverse(wDeviceID)
  508. WORD wDeviceID;   
  509.  
  510. {
  511.      DWORD dwRes;
  512.      //MCI_DGV_STEP_PARMS mciStep;
  513.                        
  514.      if (!wDeviceID)
  515.           return FALSE;                  
  516.                        
  517.      dwRes=mciSendCommand(wDeviceID, MCI_STEP, MCI_DGV_STEP_REVERSE, (DWORD)(LPVOID)NULL);
  518.  
  519.      if (dwRes)
  520.           {
  521.            return FALSE;
  522.           }
  523.  
  524.      return TRUE;
  525. }
  526.  
  527. /****************************************************************************
  528.  
  529.     FUNCTION  :  UpdateVFW(WORD, HDC)
  530.  
  531.     PURPOSE   :  This function takes a currently open device ID and 
  532.         a handle to a device context refreshing the current frame in the window.
  533.  
  534.     COMMENTS  :  
  535.  
  536.     HISTORY   :
  537.  
  538. ****************************************************************************/
  539.  
  540.  
  541. BOOL FAR PASCAL UpdateVFW(wDeviceID,hDC)
  542. WORD wDeviceID;
  543. HDC  hDC;
  544. {
  545.      DWORD dwRes;
  546.      MCI_DGV_UPDATE_PARMS mciUpdate;
  547.                        
  548.      if (!wDeviceID)
  549.           return FALSE;                  
  550.                        
  551.      mciUpdate.hDC=hDC;
  552.  
  553.      dwRes=mciSendCommand(wDeviceID, MCI_UPDATE, MCI_DGV_UPDATE_HDC, (DWORD)(LPMCI_DGV_UPDATE_PARMS)&mciUpdate);
  554.  
  555.      if (dwRes)
  556.           {
  557.            ErrorProc(dwRes);
  558.            return FALSE;
  559.           }
  560.  
  561.      return TRUE;
  562. }
  563.  
  564.  
  565. /****************************************************************************
  566.  
  567.     FUNCTION  :  PauseVFWFile(WORD)
  568.  
  569.     PURPOSE   :  This function takes a currently open device ID and Pauses the 
  570.     AVI file at the current frame.
  571.          
  572.  
  573.     COMMENTS  :  
  574.  
  575.     HISTORY   :
  576.  
  577. ****************************************************************************/
  578.  
  579.  
  580. BOOL FAR PASCAL PauseVFWFile(wDeviceID)
  581. WORD wDeviceID;
  582. {
  583.      DWORD dwRes;
  584.                        
  585.      if (!wDeviceID)
  586.           return FALSE;                  
  587.                        
  588.      dwRes=mciSendCommand(wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID)NULL);
  589.  
  590.      if (dwRes)
  591.           {
  592.            ErrorProc(dwRes);
  593.            return FALSE;
  594.            } 
  595.  
  596.      return TRUE;
  597. }
  598.  
  599.  
  600. /****************************************************************************
  601.  
  602.     FUNCTION  :  StopVFWFile(HWND,WORD,LPSTR)
  603.  
  604.     PURPOSE   :  This function takes a currently open device ID and stops the AVI file
  605.     on the current frame.
  606.  
  607.     COMMENTS  :  
  608.  
  609.     HISTORY   :
  610.  
  611. ****************************************************************************/
  612.  
  613.  
  614. BOOL FAR PASCAL StopVFWFile(wDeviceID)
  615. WORD wDeviceID;
  616. {
  617.      DWORD dwRes;
  618.                        
  619.      if (!wDeviceID)
  620.           return FALSE;                  
  621.                        
  622.      dwRes=mciSendCommand(wDeviceID, MCI_STOP, 0, (DWORD)(LPVOID)NULL);
  623.  
  624.      if (dwRes)
  625.           {
  626.            ErrorProc(dwRes);
  627.            return FALSE;
  628.           }
  629.  
  630.      return TRUE;
  631. }
  632.  
  633. /****************************************************************************
  634.  
  635.     FUNCTION  :  ResumeVFWFile(HWND,WORD,LPSTR)
  636.  
  637.     PURPOSE   :  This function takes a currently open device ID and resumes the AVI file
  638.     where it left off.
  639.  
  640.     COMMENTS  :  
  641.  
  642.     HISTORY   :
  643.  
  644. ****************************************************************************/
  645.  
  646.  
  647. BOOL FAR PASCAL ResumeVFWFile(wDeviceID)
  648. WORD wDeviceID;
  649. {
  650.      DWORD dwRes;
  651.                        
  652.      if (!wDeviceID)
  653.           return FALSE;
  654.                             
  655.      dwRes=mciSendCommand(wDeviceID, MCI_RESUME, 0, (DWORD)(LPVOID)NULL);
  656.  
  657.      if (dwRes)
  658.           {
  659.            ErrorProc(dwRes);
  660.            return FALSE;
  661.           }
  662.  
  663.      return TRUE;
  664. }
  665.  
  666.  
  667.  
  668. /****************************************************************************
  669.  
  670.     FUNCTION  :  CloseVFWDevice(HWND,WORD)
  671.  
  672.     PURPOSE   :  This function closes a currently open AVI device.
  673.  
  674.  
  675.     COMMENTS  :  To conserve system resources you should always close a
  676.          device when you are not using it.
  677.  
  678.     HISTORY   :
  679.  
  680. ****************************************************************************/
  681.  
  682.  
  683. BOOL FAR PASCAL CloseVFWDevice(hWnd,wGlobalDeviceID)
  684.  
  685. HWND hWnd;
  686. WORD wGlobalDeviceID;
  687.  
  688. {
  689.      DWORD dwRes;
  690.      
  691.      if (!wGlobalDeviceID)
  692.           return FALSE;
  693.      // Close the global device
  694.  
  695.       dwRes=mciSendCommand(wGlobalDeviceID,MCI_CLOSE,0,NULL);
  696.  
  697.      if (dwRes)
  698.          {
  699.           ErrorProc(dwRes);
  700.           return(FALSE);
  701.           }
  702.  
  703.  
  704.  
  705.      return(TRUE);
  706.      }
  707.  
  708.  
  709.  
  710. /****************************************************************************
  711.  
  712.     FUNCTION  :  OpenPlayCloseVFW(HWND,HWND,LPSTR)
  713.  
  714.     PURPOSE   :  This function demonstrates how to open an element (avifile)
  715.          play the AVI video and close that element in one fell swoop.
  716.  
  717.     COMMENTS  :  Rather than opening the global device "avivideo" before
  718.          playing the AVI file we let MCI do this for us.  After playing
  719.          the element we immediately close the element and MCI closes
  720.          the global device "avivideo" for you.  Unfortunately doing
  721.          this is slow and is not advisable if you will be playing several
  722.          AVI files at a time.  If you plan on playing several AVI files
  723.          at a time it is advisable for speed to use the previously
  724.          demonstrated method.  Which is opening the global avivideo device.
  725.          Leaving that device open and passing it to the open for the element
  726.          (avifile).  Playing all the avifiles and then closing the global
  727.          avivideo device when you are finished.
  728.  
  729.     HISTORY   :
  730.  
  731. ****************************************************************************/
  732.  
  733.  
  734. BOOL FAR PASCAL OpenPlayCloseVFW(hWnd,hWnd1,lpVFWfile)
  735.  
  736. HWND hWnd;
  737. HWND hWnd1;
  738. LPSTR lpVFWfile;
  739.  
  740.  
  741. {
  742.      MCI_DGV_OPEN_PARMS mciopen;
  743.      MCI_PLAY_PARMS mciplay;
  744.      DWORD dwRes;
  745.      WORD wDeviceID;
  746.      //WORD wGlobalDeviceID;
  747.      DWORD dwFlags;
  748.      MCI_DGV_WINDOW_PARMS mciWindow;
  749.  
  750.  
  751.  
  752.  
  753.      mciopen.lpstrDeviceType=(LPSTR)"avivideo";
  754.      mciopen.lpstrElementName=(LPSTR)lpVFWfile;
  755.  
  756.    
  757.      dwFlags=MCI_OPEN_TYPE | MCI_OPEN_ELEMENT;
  758.  
  759.  
  760.  
  761.      dwRes=mciSendCommand(0,MCI_OPEN,dwFlags,(DWORD)(LPSTR)&mciopen);
  762.  
  763.      if (dwRes)
  764.        {
  765.        ErrorProc(dwRes);
  766.        return FALSE;
  767.        }
  768.  
  769.      wDeviceID=mciopen.wDeviceID;
  770.  
  771.      mciWindow.hWnd=hWnd1;
  772.      dwRes=mciSendCommand(wDeviceID,MCI_WINDOW,MCI_DGV_WINDOW_HWND,(DWORD)(LPSTR)&mciWindow);
  773.  
  774.      if (dwRes)
  775.        {
  776.        ErrorProc(dwRes);
  777.        return FALSE;
  778.        }
  779.  
  780.  
  781.      mciplay.dwFrom=0;
  782.      dwFlags=MCI_WAIT | MCI_FROM;
  783.      dwRes=mciSendCommand(wDeviceID,MCI_PLAY,dwFlags,(DWORD)(LPSTR)&mciplay);
  784.  
  785.      if (dwRes)
  786.        {
  787.        ErrorProc(dwRes);
  788.        return FALSE;
  789.        }
  790.  
  791.      mciSendCommand(wDeviceID,MCI_CLOSE,0L,NULL);
  792.  
  793.      return TRUE;
  794. }
  795.  
  796.  
  797. /****************************************************************************
  798.  
  799.     FUNCTION  :  SndPlaySnd(HWND,LPSTR)
  800.  
  801.     PURPOSE   :  This function demonstrates how to use the function
  802.          sndPlaySound.  This is the easiest way to play a wave file.
  803.  
  804.     COMMENTS  :  The one setback to using sndPlaySound is that the entire
  805.          sound is loaded into memory at once.  If you have a very
  806.          large sound sndPlaySound will fail if the sound will not
  807.          load into physical memory.
  808.  
  809.     HISTORY   :
  810.  
  811. ****************************************************************************/
  812.  
  813. BOOL FAR PASCAL SndPlaySnd(hWnd,lpWavefile)
  814.  
  815. HWND hWnd;
  816. LPSTR lpWavefile;
  817.  
  818.  
  819. {
  820.      WORD wFlags;
  821.  
  822.  
  823.  
  824.      MessageBox(NULL,lpWavefile,(LPSTR)"Playing Sound File",MB_OK);
  825.  
  826.  
  827.      wFlags=SND_ASYNC;
  828.  
  829.      if (sndPlaySound(lpWavefile,wFlags))
  830.        return(TRUE);
  831.      else
  832.        return(FALSE);
  833.  
  834. }
  835.  
  836.  
  837. /****************************************************************************
  838.  
  839.     FUNCTION  :  ErrorProc(WORD)
  840.  
  841.     PURPOSE   :  ErrorProc calls mciGetErrorString to display an error
  842.          message returned by MCI.
  843.  
  844.     COMMENTS  :
  845.  
  846.     HISTORY   :
  847.  
  848. ****************************************************************************/
  849.  
  850. void FAR PASCAL ErrorProc(dwResult)
  851.  
  852. DWORD dwResult;
  853.  
  854. {
  855.      HANDLE hMem;
  856.      LPSTR lpStringBuff;
  857.  
  858.  
  859.      hMem=GlobalAlloc(GHND,80);
  860.  
  861.      if (hMem)
  862.       {
  863.        lpStringBuff=GlobalLock(hMem);
  864.        if (lpStringBuff)
  865.         {
  866.          if(mciGetErrorString(dwResult,lpStringBuff,80))
  867.                MessageBox(NULL,lpStringBuff,"ERROR",MB_OK);
  868.          else
  869.                MessageBox(NULL,"Generic Error","ERROR",MB_OK);
  870.  
  871.          GlobalUnlock(hMem);
  872.         }
  873.         else
  874.         MessageBox(NULL,"Lock Failed","ERROR",MB_OK);
  875.  
  876.         GlobalFree(hMem);
  877.         }
  878.     else
  879.         MessageBox(NULL,"Alloc Failed","ERROR",MB_OK);
  880.  
  881.     return;
  882. }
  883.  
  884.  
  885.  
  886. /****************************************************************************
  887.  
  888.     FUNCTION  :  OpenWaveDevice(HWND)
  889.  
  890.     PURPOSE   :  This function opens the device waveaudio and leaves it open.
  891.          The device could also be opened by assigning the string
  892.          "waveaudio" as follows.
  893.  
  894.          mciopen.lpstrDeviceType="waveaudio"
  895.  
  896.          Using this format you would only have to specify
  897.  
  898.          MCI_OPEN_TYPE for dwflags.
  899.  
  900.     COMMENTS  :  Opening the driver once and leaving it open saves time.
  901.          You can then use this driver ID in the following calls
  902.          to open elements (wavefiles).    This saves time since if
  903.          you open an element without specifying an open device ID
  904.          mci must open the driver and close the driver each time.
  905.          When playing multiple elements (wavefiles) this excess time
  906.          can be costly.  Therefore to conserve time you should open
  907.          the waveaudio device, play the waves, close the waveaudio
  908.          device.
  909.  
  910.  
  911.     HISTORY   :
  912.  
  913. ****************************************************************************/
  914.  
  915.  
  916. WORD FAR PASCAL OpenWaveDevice(void)
  917.  
  918. {
  919.  
  920.      MCI_OPEN_PARMS mciopen;
  921.      DWORD dwRes;
  922.      WORD wGlobalDeviceID;
  923.      DWORD dwFlags;
  924.  
  925.  
  926.      mciopen.wDeviceID=0;
  927.      mciopen.lpstrDeviceType=(LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
  928.  
  929.  
  930.      // when specifying the device ID you must also include the
  931.      //MCI_OPEN_TYPE flag.
  932.  
  933.  
  934.      dwFlags= MCI_OPEN_TYPE_ID | MCI_OPEN_TYPE;
  935.  
  936.      dwRes=mciSendCommand(0,MCI_OPEN,dwFlags,(DWORD)(LPSTR)&mciopen);
  937.  
  938.      if (dwRes)
  939.     {
  940.     ErrorProc(dwRes);
  941.     return(FALSE);
  942.     }
  943.  
  944.      wGlobalDeviceID=mciopen.wDeviceID;
  945.  
  946.      return(wGlobalDeviceID);
  947.      }
  948.  
  949.  
  950. /****************************************************************************
  951.  
  952.     FUNCTION  :  OpenWaveFile(HWND,WORD,LPSTR)
  953.  
  954.     PURPOSE   :  This function takes a currently open device ID and a
  955.          string representing a wave file and plays that wavefile.
  956.          When the wavefile is finished playing the function closes
  957.          the wavefile.
  958.  
  959.     COMMENTS  :  dwFlags is set to MCI_WAIT so that the function will not
  960.          return until the wavefile is done playing.  If you want
  961.          the function to return before the wavefile is finished
  962.          playing omit the MCI_WAIT flag.
  963.  
  964.     HISTORY   :
  965.  
  966. ****************************************************************************/
  967.  
  968.  
  969. WORD FAR PASCAL OpenWaveFile(wGlobalDeviceID,lpWaveName,lpAlias)
  970.  
  971. WORD wGlobalDeviceID;
  972. LPSTR lpWaveName;
  973. LPSTR lpAlias;
  974. {
  975.  
  976.      MCI_OPEN_PARMS mciopen;
  977.      //MCI_PLAY_PARMS mciplay;
  978.      DWORD dwRes;
  979.      WORD wDeviceID;
  980.      DWORD dwFlags;
  981.      //char szWaveMessage[80];
  982.  
  983.  
  984.      
  985.      if (!wGlobalDeviceID)
  986.           return FALSE;
  987.      
  988.      
  989.      // Open the wave file independent of the device.    This will speed up
  990.      // the playing of the wavefile.
  991.  
  992.                  
  993.                  
  994.  
  995.      dwFlags= MCI_OPEN_ELEMENT | MCI_OPEN_ALIAS;
  996.  
  997.      mciopen.lpstrElementName=(LPSTR)lpWaveName;
  998.  
  999.      // All strings must be initialized otherwise this will crash under
  1000.      // the debug version of mmsystem.
  1001.  
  1002.      mciopen.lpstrDeviceType="\0";
  1003.      mciopen.lpstrAlias=(LPSTR)lpAlias;
  1004.  
  1005.      dwRes=mciSendCommand(wGlobalDeviceID,MCI_OPEN,dwFlags,(DWORD)(LPSTR)&mciopen);
  1006.  
  1007.  
  1008.      if (dwRes)
  1009.     {
  1010.     ErrorProc(dwRes);
  1011.     return FALSE;
  1012.     }
  1013.  
  1014.      // play the wave file.
  1015.  
  1016.      wDeviceID=mciopen.wDeviceID;
  1017.  
  1018.  
  1019.      return wDeviceID;
  1020.      }
  1021.  
  1022.  
  1023.  
  1024.  
  1025. /****************************************************************************
  1026.  
  1027.     FUNCTION  :  PlayWave(HWND,WORD,LPSTR)
  1028.  
  1029.     PURPOSE   :  This function takes a currently open device ID and a
  1030.          string representing a wave file and plays that wavefile.
  1031.          When the wavefile is finished playing the function closes
  1032.          the wavefile.
  1033.  
  1034.     COMMENTS  :  dwFlags is set to MCI_WAIT so that the function will not
  1035.          return until the wavefile is done playing.  If you want
  1036.          the function to return before the wavefile is finished
  1037.          playing omit the MCI_WAIT flag.
  1038.  
  1039.     HISTORY   :
  1040.  
  1041. ****************************************************************************/
  1042.  
  1043.  
  1044. BOOL FAR PASCAL PlayWaveFile(wDeviceID)
  1045.  
  1046. WORD wDeviceID;
  1047.  
  1048. {
  1049.  
  1050.      //MCI_OPEN_PARMS mciopen;
  1051.      MCI_PLAY_PARMS mciplay;
  1052.      DWORD dwRes;
  1053.      DWORD dwFlags;
  1054.      //char szWaveMessage[80];
  1055.  
  1056.  
  1057.      if (!wDeviceID)
  1058.           return FALSE;            
  1059.                  
  1060.                  
  1061.      // Open the wave file independent of the device.    This will speed up
  1062.      // the playing of the wavefile.
  1063.  
  1064.  
  1065.  
  1066.      mciplay.dwFrom=0; 
  1067.     
  1068.  
  1069.      dwFlags=MCI_FROM;
  1070.  
  1071.  
  1072.      dwRes=mciSendCommand(wDeviceID,MCI_PLAY,dwFlags,(DWORD)(LPSTR)&mciplay);
  1073.  
  1074.      if (dwRes)
  1075.           {
  1076.            ErrorProc(dwRes);
  1077.            return FALSE;
  1078.           } 
  1079.        
  1080.  
  1081.      return TRUE;
  1082.      }
  1083.  
  1084.           
  1085.      /****************************************************************************
  1086.  
  1087.     FUNCTION  :  PlayWave(HWND,WORD,LPSTR)
  1088.  
  1089.     PURPOSE   :  This function takes a currently open device ID and a
  1090.          string representing a wave file and plays that wavefile.
  1091.          When the wavefile is finished playing the function closes
  1092.          the wavefile.
  1093.  
  1094.     COMMENTS  :  dwFlags is set to MCI_WAIT so that the function will not
  1095.          return until the wavefile is done playing.  If you want
  1096.          the function to return before the wavefile is finished
  1097.          playing omit the MCI_WAIT flag.
  1098.  
  1099.     HISTORY   :
  1100.  
  1101. ****************************************************************************/
  1102.  
  1103.  
  1104. BOOL FAR PASCAL StopWaveFile(wDeviceID)
  1105.  
  1106. WORD wDeviceID;
  1107. {
  1108.  
  1109.      
  1110.     
  1111.      DWORD dwRes;
  1112.      DWORD dwFlags;
  1113.      
  1114.  
  1115.      if (!wDeviceID)
  1116.           return FALSE;                  
  1117.                        
  1118.  
  1119.      // Open the wave file independent of the device.    This will speed up
  1120.      // the playing of the wavefile.
  1121.      
  1122.      dwFlags=MCI_WAIT;
  1123.  
  1124.      dwRes=mciSendCommand(wDeviceID,MCI_STOP,dwFlags,(DWORD)(LPVOID)NULL);
  1125.  
  1126.      if (dwRes)
  1127.           {
  1128.            ErrorProc(dwRes);
  1129.            return FALSE;
  1130.           }
  1131.  
  1132.  
  1133.      return TRUE;
  1134.      }
  1135.      
  1136.           
  1137. /****************************************************************************
  1138.  
  1139.     FUNCTION  :  CloseWaveFile(HWND,WORD,LPSTR)
  1140.  
  1141.     PURPOSE   :  This function takes a currently open device ID and a
  1142.          string representing a wave file and plays that wavefile.
  1143.          When the wavefile is finished playing the function closes
  1144.          the wavefile.
  1145.  
  1146.     COMMENTS  :  dwFlags is set to MCI_WAIT so that the function will not
  1147.          return until the wavefile is done playing.  If you want
  1148.          the function to return before the wavefile is finished
  1149.          playing omit the MCI_WAIT flag.
  1150.  
  1151.     HISTORY   :
  1152.  
  1153. ****************************************************************************/
  1154.  
  1155.  
  1156. BOOL FAR PASCAL CloseWaveFile(wDeviceID)
  1157.  
  1158. WORD wDeviceID;
  1159. {
  1160.  
  1161.      DWORD dwRes;
  1162.  
  1163.      if (!wDeviceID)
  1164.           return FALSE;
  1165.  
  1166.      // Open the wave file independent of the device.    This will speed up
  1167.      // the playing of the wavefile.
  1168.  
  1169.      dwRes=mciSendCommand(wDeviceID,MCI_CLOSE,0,NULL);
  1170.  
  1171.      if (dwRes)
  1172.      {
  1173.      ErrorProc(dwRes);
  1174.      return FALSE;
  1175.      }
  1176.  
  1177.      return TRUE;
  1178.      }
  1179.  
  1180.  
  1181. /****************************************************************************
  1182.  
  1183.     FUNCTION  :  CloseWaveDevice(HWND,WORD)
  1184.  
  1185.     PURPOSE   :  This function closes a currently open waveaudio device.
  1186.  
  1187.  
  1188.     COMMENTS  :     To conserve system resources you should always close a
  1189.          device when you are not using it.
  1190.  
  1191.     HISTORY   :
  1192.  
  1193. ****************************************************************************/
  1194.  
  1195.  
  1196. BOOL FAR PASCAL CloseWaveDevice(wGlobalDeviceID)
  1197.  
  1198. WORD wGlobalDeviceID;
  1199.  
  1200. {
  1201.      DWORD dwRes;
  1202.      
  1203.      if (!wGlobalDeviceID)
  1204.           return FALSE;                  
  1205.                        
  1206.      // Close the global device
  1207.  
  1208.       dwRes=mciSendCommand(wGlobalDeviceID,MCI_CLOSE,0,NULL);
  1209.  
  1210.      if (dwRes)
  1211.     {
  1212.     ErrorProc(dwRes);
  1213.     return(FALSE);
  1214.     }
  1215.  
  1216.  
  1217.  
  1218.      return(TRUE);
  1219.      }     
  1220.      
  1221. /****************************************************************************
  1222.  
  1223.     FUNCTION   : cNumAudio
  1224.  
  1225.     PARAMETERS : void
  1226.  
  1227.     PURPOSE    : retrieves the number of waveform output devices
  1228.          present in the system.
  1229.  
  1230.     CALLS      : WINDOWS
  1231.            GetModuleHandle
  1232.            waveOutGetNumDevs
  1233.  
  1234.     RETURNS    : number of wave output devices found
  1235.  
  1236.     MESSAGES   : none
  1237.  
  1238.     COMMENTS   : first checks to see if MMSYSTEM is loaded.  So it works
  1239.          in 3.0 as well as 3.1
  1240.  
  1241.     HISTORY    : 8/31/93 - Function added to playvfw by stevemo
  1242.  
  1243. ****************************************************************************/
  1244.  
  1245. int PASCAL cNumAudio(void)
  1246. {
  1247.    UINT (FAR *lpfnwaveOutGetNumDevs) (void);
  1248.    HINSTANCE hInstWave;
  1249.    int         nRet = 0;
  1250.    HANDLE    hModule = GetModuleHandle("mmsystem.dll");
  1251.  
  1252.    if (hModule)
  1253.    {
  1254.      hInstWave = LoadLibrary("MMSYSTEM.DLL");
  1255.      if (hInstWave > HINSTANCE_ERROR)
  1256.      {
  1257.        (FARPROC) lpfnwaveOutGetNumDevs =
  1258.           GetProcAddress(hInstWave, "waveOutGetNumDevs");
  1259.        nRet = (*lpfnwaveOutGetNumDevs) ();
  1260.        FreeLibrary(hInstWave);
  1261.      }
  1262.    }
  1263.    return (nRet);
  1264. }     
  1265.